using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YayiMgr.Ability.CommonApp;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.ManufacturersApp.Dto;
using YayiMgr.Domain.BasicData;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.ManufacturersApp
{
    public class ManufacturersServicer: AbilityServicer
    {
        public IRepository<Manufacturers> RepoManufacturers { get; set; }
        public IRepository<Suppliers> RepoSuppliers { get; set; }

        public async Task<QueryListDto<ManufacturersDto>> GetAllManufacturers()
        {
            var query = await RepoManufacturers.WithJoinAsync<SupplierFacturerMapping, Suppliers>(
                await RepoSuppliers.GetListAsync(), s=> s.Manufacturers
            );
            QueryListDto<ManufacturersDto> page = QueryListDto<ManufacturersDto>.Create(null, 1,1,1);
            return page;
        }

        public async Task<QueryListDto<ManufacturersDto>> GetManufacturers(ManufacturersQueryDto dto)
        {
            // IQueryable<Manufacturers> manufacturers = RepoManufacturers.AsQueryable();
            // IQueryable<Suppliers> suppliers = RepoSuppliers.AsQueryable().Where(s => s.CompanyId == dto.CompanyId);

            if (dto == null)
            var query = await RepoManufacturers.Where().WithJoinAsync<SupplierFacturerMapping, Suppliers>(
                await RepoSuppliers.GetListAsync(s => s.CompanyId == dto.CompanyId), s=> s.Manufacturers    // && s.Id == dto.SuppliersId
            );
            QueryListDto<ManufacturersDto> page = QueryListDto<ManufacturersDto>.Create(null, 1,1,1);
            return page;
        }






        public async Task<ResponseDto<ManufacturersDto>> AddManufacturers(ManufacturersCreateDto dto)
        {
            Manufacturers data = ModelMapper.Map<ManufacturersCreateDto, Manufacturers>(dto);
            data = await RepoManufacturers.InsertAsync(data);
            ManufacturersDto dto2 = ModelMapper.Map<Manufacturers, ManufacturersDto>(data);
            return ResponseDto<ManufacturersDto>.CreateSuccess(dto2);
        }

        public async Task<ResponseDto<ManufacturersDto>> UpdateManufacturers(Guid id, ManufacturersUpdateDto dto)
        {
            Manufacturers data = await RepoManufacturers.GetAsync(id);
            if (data == null)
            {
                return ResponseDto<ManufacturersDto>.CreateFail("无数据");
            }
            data.UpdateEntity(ModelMapper.Map<ManufacturersUpdateDto, Manufacturers>(dto));
            data = await RepoManufacturers.UpdateAsync(data);
            ManufacturersDto dto2 = ModelMapper.Map<Manufacturers, ManufacturersDto>(data);
            return ResponseDto<ManufacturersDto>.CreateSuccess(dto2);
        }

        public async Task<ResponseDto<Object>> DeleteManufacturers(Guid id)
        {
            Manufacturers data = await RepoManufacturers.GetAsync(id);
            if (data == null)
            {
                return ResponseDto<Object>.CreateFail("无数据");
            }
            await RepoManufacturers.DeleteAsync(data);
            return ResponseDto<Object>.CreateSuccess(null);
        }
    }
}